package com.cremamobile.filemanager;

import com.cremamobile.filemanager.file.FileUtils;
import com.cremamobile.filemanager.file.Exception.ExistSameFileNameException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class SettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
	VersionPreference mVersion;
	ListPreference mLanguage;
	CheckBoxPreference mLastFolder;
	Preference mAbout;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
	   addPreferencesFromResource(R.xml.preference);
	   
	 //get UI object references
       PreferenceScreen prefSet = getPreferenceScreen();

       mVersion = (VersionPreference) prefSet.findPreference(getResources().getString(R.string.pref_key_version));
       mVersion.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				return false;
			}
       });
       
       mLanguage = (ListPreference) prefSet.findPreference(getResources().getString(R.string.pref_key_lang));
//       mLanguage.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
//		@Override
//		public boolean onPreferenceChange(Preference preference, Object newValue) {
//			// TODO Auto-generated method stub
//			String lang = (String) newValue;
//			return true;
//		}
//       });   
       
       mLastFolder = (CheckBoxPreference) prefSet.findPreference(getResources().getString(R.string.pref_key_last_folder));
//       mLastFolder.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
//		@Override
//		public boolean onPreferenceChange(Preference preference, Object newValue) {
//			// TODO Auto-generated method stub
//			
//			return false;
//		}
//		});

       mAbout = (Preference) prefSet.findPreference(getResources().getString(R.string.pref_key_about));
       mAbout.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				CremaDialog dialog = new CremaDialog(SettingActivity.this, CremaDialog.TYPE_ABOUT);
				dialog.show();
				return true;
			}
       });
       
       getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	@Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(getResources().getString(R.string.pref_key_lang))) {
    		String locale = sharedPreferences.getString(key, null);
    		if (locale != null) {
    			CremaActivity.updateLanguage(this, locale);
                restartActivity();
    		}
        }
    }
	
	@Override
    protected void onStop() {
        super.onStop();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
	
	private void restartActivity() {
	    Intent intent = getIntent();
	    finish();
	    startActivity(intent);
	}
}
